package com.byt.framlib.baseadapter.rv;

/**
 * Created by tom on 2017/10/27.
 */

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

public class RvMultiItemTypeAdapter<T> extends RecyclerView.Adapter<RvViewHolder> {
    protected Context mContext;
    protected List<T> mDatas;

    protected RvItemViewDelegateManager mRvItemViewDelegateManager;
    protected OnItemClickListener mOnItemClickListener;


    public RvMultiItemTypeAdapter(Context context, List<T> datas) {
        mContext = context;
        mDatas = datas;
        mRvItemViewDelegateManager = new RvItemViewDelegateManager();
    }

    @Override
    public int getItemViewType(int position) {
        if (!useItemViewDelegateManager()) return super.getItemViewType(position);
        return mRvItemViewDelegateManager.getItemViewType(mDatas.get(position), position);
    }


    @Override
    public RvViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RvItemViewDelegate itemViewDelegate = mRvItemViewDelegateManager.getItemViewDelegate(viewType);
        int layoutId = itemViewDelegate.getItemViewLayoutId();
        RvViewHolder holder = RvViewHolder.createViewHolder(mContext, parent, layoutId);
        onViewHolderCreated(holder, holder.getConvertView());
        setListener(parent, holder, viewType);
        return holder;
    }

    public void onViewHolderCreated(RvViewHolder holder, View itemView) {

    }

    public void convert(RvViewHolder holder, T t) {
        mRvItemViewDelegateManager.convert(holder, t, holder.getAdapterPosition());
    }

    protected boolean isEnabled(int viewType) {
        return true;
    }

    protected void setListener(final ViewGroup parent, final RvViewHolder rvViewHolder, int viewType) {
        if (!isEnabled(viewType)) return;
        rvViewHolder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    int position = rvViewHolder.getAdapterPosition();
                    mOnItemClickListener.onItemClick(v, rvViewHolder, position);
                }
            }
        });

        rvViewHolder.getConvertView().setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnItemClickListener != null) {
                    int position = rvViewHolder.getAdapterPosition();
                    return mOnItemClickListener.onItemLongClick(v, rvViewHolder, position);
                }
                return false;
            }
        });
    }

    @Override
    public void onBindViewHolder(RvViewHolder holder, int position) {
        convert(holder, mDatas.get(position));
    }

    @Override
    public int getItemCount() {
        int itemCount = mDatas.size();
        return itemCount;
    }

    public List<T> getDatas() {
        return mDatas;
    }

    public RvMultiItemTypeAdapter addItemViewDelegate(RvItemViewDelegate<T> itemViewDelegate) {
        mRvItemViewDelegateManager.addDelegate(itemViewDelegate);
        return this;
    }

    public RvMultiItemTypeAdapter addItemViewDelegate(int viewType, RvItemViewDelegate<T> itemViewDelegate) {
        mRvItemViewDelegateManager.addDelegate(viewType, itemViewDelegate);
        return this;
    }

    protected boolean useItemViewDelegateManager() {
        return mRvItemViewDelegateManager.getItemViewDelegateCount() > 0;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, RecyclerView.ViewHolder holder, int position);

        boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }
}
